home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!news
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Beginner C++ Programmer has 1 More Question
- Date: 4 Jan 1996 19:47:18 GMT
- Organization: Datalytics, Inc
- Message-ID: <4chas6$ldg@gold.datalytics.com>
- References: <4bskl1$ain@ixnews5.ix.netcom.com>
- NNTP-Posting-Host: pc071.datalytics.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- psilocyn@ix.netcom.com(Jeff W. ) wrote:
- [snip]
- >and stuff. I have an array, ScoresArray[21], which I use to hold 21
- >high scores read in from a file. What I need to do is a few things.
- >1)if the player's score makes the high score list, insert it in the
- >appropriate position in ScoresArray[21] (0=highest score, 20=lowest
- >score), and then adjust the array so that the scores are shifted down.
- >For example, if I had a smaller array, SmallArray[4], filled with:
- >100
- >90
- >80
- >70
- >And the player's score is 95, I want to insert 95 between 100 and 90,
- >place 100 back into SmallArray[0], 95 into SmallArray[1], 90 into [2],
- >and 80 into [3] while discarding 70. Does anyone have any idea as to
- >how to go about doing this? Thanks.
-
- With so few elements in the array, it may not be appropriate
- to implement a binary search algorithm. Therefore, I'll
- concentrate on the brute force, sequential search approach.
-
- const size_t MAX_SCORES = 21;
- size_t vScores[MAX_SCORES];
-
- for (size_t i = 0; i < MAX_SCORES; i++)
- {
- if (vScores[i] < score)
- {
- for (size_t j = MAX_SCORES - 1; j > i; j--)
- {
- vScores[j] = vScores[j - 1];
- }
- vScores[i] = score;
- }
- }
-
- This assumes you initialize the array to all zeroes so the
- initial MAX_SCORES scores greater than zero will be inserted.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc.
- (513)226-7700
- stew@datalytics.com
-
-
-